package org.fintool.http;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;



public class HTTPUtil {

	static long DELAY_TIME=5000L;


	public final  String getHTTPContentAsString(URL webURL)throws Exception{

		StringBuffer sb=null;

		HttpURLConnection connection=null;

		BufferedReader reader=null;
		String inputLine=null;

		delay();

		if(webURL!=null){
			sb=new StringBuffer();
			connection=(HttpURLConnection)webURL.openConnection();



			reader= new BufferedReader(new InputStreamReader(connection.getInputStream()));


			if(reader!=null){
				while ((inputLine = reader.readLine()) != null) {
					// Process each line.
					//if(!(inputLine.indexOf("<!DOCTYPE")>=0)){
					sb.append(inputLine+"\r\n");
					//}
				}

			}









		}
		reader.close();

		return sb.toString();
	}



public final  String getHTTPSContentAsString(URL webURL)throws Exception{

	StringBuffer sb=null;

	HttpsURLConnection connection=null;

	BufferedReader reader=null;
	String inputLine=null;


	delay();

	if(webURL!=null){
		sb=new StringBuffer();
		connection=(HttpsURLConnection)webURL.openConnection();



		reader= new BufferedReader(new InputStreamReader(connection.getInputStream()));


		if(reader!=null){
			while ((inputLine = reader.readLine()) != null) {
				// Process each line.
				//if(!(inputLine.indexOf("<!DOCTYPE")>=0)){
				sb.append(inputLine+"\r\n");
				//}
			}

		}









	}
	reader.close();

	return sb.toString();
}

private static  final void delay(){
	try
    {
        // Using Thread.sleep() we can add delay in our application in
        // a millisecond time. For the example below the program will
        // take a deep breath for one second before continue to print
        // the next value of the loop.
        Thread.sleep(DELAY_TIME);

        // The Thread.sleep() need to be executed inside a try-catch
        // block and we need to catch the InterruptedException.
    } catch (InterruptedException ie)
    {
        ie.printStackTrace();
    }
}


//test main
public static void main(String[] args) throws Exception{
	//System.out.println(new HTTPUtil().getHTTPContentAsString((new URL("http://www.google.com/finance?chdnp=1&chdd=1&chds=1&chdv=1&chvs=Logarithmic&chdeh=0&chfdeh=0&chdet=1301466580839&chddm=361&chls=IntervalBasedLine&q=INDEXASX:XMM&"))));

	System.out.println(new HTTPUtil().getHTTPContentAsString(new URL(new URLMaker().getURL("MAM", "ASX"))));

}




}